#include <SPI.h>
#include <MFRC522.h>

#define FREQ 50        //舵机频率
#define CHANNEL 1      //pwm通道
#define RESOLUTION 10  //2**10分辨率（把周期分成2**10份）
#define SERVO 9        //舵机引脚GPIO9

#define RST_PIN 6      //RC522rst引脚
#define SS_PIN 7       //RC522ss引脚

byte readCard[4];      //读取id卡号 
//byte cardPosition = 0;
byte Card1[4] = {0x53,0xB1,0xD7,0x0C};      //存放id卡号
byte Card2[4] = {0x73,0x7B,0x12,0x1A};
byte Card3[4] = {0x68,0x60,0x4B,0xDD};
byte Card4[4] = {0x9C,0x3A,0x64,0x4B};
byte Card5[4] = {0x9A,0xED,0x55,0xE4};
byte Card6[4] = {0x82,0xEB,0xC3,0xFC};
byte Card7[4] = {0x82,0x33,0x2E,0x99};
byte Card8[4] = {0x06,0x2E,0x0D,0x60};

MFRC522 mfrc522(SS_PIN, RST_PIN);

void setup() {
  Serial.begin(9600);
  while (!Serial)
    ;

  SPI.begin(2, 10, 3, 7);
  mfrc522.PCD_Init();
  delay(4);
  mfrc522.PCD_DumpVersionToSerial();

  ledcSetup(CHANNEL, FREQ, RESOLUTION);
  ledcAttachPin(SERVO, CHANNEL);
}

void loop() {
  if (!mfrc522.PICC_IsNewCardPresent()) {
    return;
  }
  if (!mfrc522.PICC_ReadCardSerial()) {
    return;
  }

  Serial.println(F("The card ID is:"));
  for (uint8_t i = 0; i < 4; i++) {
    readCard[i] = mfrc522.uid.uidByte[i];
    Serial.print(readCard[i], HEX);
    Serial.print(" ");

    delay(20);
   // cardPosition += 3;
  }
  if (!memcmp(readCard,Card1,4)||!memcmp(readCard,Card2,4)||!memcmp(readCard,Card3,4)||!memcmp(readCard,Card4,4)||!memcmp(readCard,Card5,4)||!memcmp(readCard,Card6,4)||!memcmp(readCard,Card7,4)||!memcmp(readCard,Card8,4)) {
    int min_degree = 0.5 / 20 * pow(2, RESOLUTION);
    int max_degree = 2.5 / 20 * pow(2, RESOLUTION);
    ledcWrite(CHANNEL, min_degree);
    delay(3000);
    ledcWrite(CHANNEL, max_degree);
    //delay(2000);
  }

  Serial.println("");
  mfrc522.PICC_HaltA();
}